const permissionModel = require("../models/permissionModel")
const jwt = require('jsonwebtoken');
// 权限添加
exports.addpermission = (req, res) => {
    let { name, father, sign, type, router_url, component_url, icon, sort } = req.body
    let p = permissionModel.createpermission(name, father, sign, type, router_url, component_url, icon, sort)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "权限添加成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "权限添加失败！", error })
    })
}
// 权限删除--软删除
exports.del_permis_f = (req, res) => {
    let { id } = req.query
    let p = permissionModel.del_permis_f(id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "权限禁用成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "权限禁用失败！", error })
    })
}

// 权限启用
exports.start = (req, res) => {
    let { id } = req.query
    let p = permissionModel.start_state(id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "权限启用成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "权限启用失败！", error })
    })
}

// 权限删除--真删除
exports.del_permis_t = (req, res) => {
    let { id } = req.query
    let p = permissionModel.del_permis_t(id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "权限删除成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "权限删除失败！", error })
    })
}
// 权限修改--获取数据
exports.permis_get = (req, res) => {
    let { id } = req.query
    let p = permissionModel.permis_get(id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "权限修改--获取数据成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "权限修改--获取数据失败！", error })
    })
}
// 权限修改--设置数据
exports.permis_set = (req, res) => {
    let { id, name, father, sign, type, router_url, component_url, icon, sort } = req.body
    let p = permissionModel.permis_set(name, father, sign, type, router_url, component_url, icon, sort, id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "权限修改--设置数据成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "权限修改--设置数据失败！", error })
    })
}
// 添加子项权限
exports.add_son_permis_c = (req, res) => {
    let { name, father, sign, type, router_url, component_url, icon, sort } = req.body
    let p = permissionModel.add_son_permis(name, father, sign, type, router_url, component_url, icon, sort)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "添加子项权限成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "添加子项权限失败！", error })
    })
}
// 列表搜索--分页
exports.permis_list_c = (req, res) => {
    let { name, sign, page, page_size } = req.query
    let p = permissionModel.permis_list(name, sign, Number(page), Number(page_size))
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "列表搜索加载成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "列表搜索加载失败！", error })
    })
}
// 全部权限接口
exports.permis_all = (req, res) => {
    let p = permissionModel.permis_all()
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "全部权限加载成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "全部权限加载失败！", error })
    })
}
// 全部权限父级接口
exports.permis_all_father = (req, res) => {
    let p = permissionModel.permis_all_father()
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "全部权限父级接口加载成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "全部权限父级接口加载失败！", error })
    })
}
// 获取用户对应的所以权限
exports.getUserPermissions = (req, res) => {
    // 从请求头中获取Authorization字段，并提取token
    // const authHeader = req.headers.authorization;
    // const token = authHeader ? authHeader.split(' ')[1] : null; // 提取token
    // if (!token) {
    //     return res.status(401).json({ code: 401, msg: "未提供token" });
    // }
    // const decoded = jwt.verify(token, '39qw89r23890');
    const id = req.user.id;
    let p = permissionModel.getUserPermissions(Number(id))
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "获取用户对应的所以权限加载成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "获取用户对应的所以权限加载失败！", error })
    })
}
// 获取角色对应的所以权限
exports.getRolePermissions = (req, res) => {
    const { role_id } = req.query
    let p = permissionModel.getRolePermissions(Number(role_id))
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "获取用户对应的所以权限加载成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "获取用户对应的所以权限加载失败！", error })
    })
}